import axios from 'axios'

// 返回在vue模板中的调用接口
var HttpUtil = {
	// 配置项目根如路径
	ChinaURLRoot: 'http://120.79.79.211/qycomic',
	LocalUrlRoot: 'http://127.0.0.1:8888/Comic/', //本地域名
	AbroadRoot: 'http://47.250.42.225:8080/qycomic', //海外域名
	isProduct: 0,
	isAborad: 0, // 1代表海外，0代表国内
	vueVersion:"1.0.15",//当前vue版本

	get: function(url, params) {
		return this.httpApi('GET', url, params)
	},
	post: function(url, params) {
		return this.httpApi('POST', url, params)
	},
	put: function(url, params) {
		return this.httpApi('PUT', url, params)
	},
	delete: function(url, params) {
		return this.httpApi('DELETE', url, params)
	},
	websocketBaseUrl: function() {
		if (this.isAborad) {
			return 'ws://120.79.79.211:80/websocket/';
		} else if (isProduct != 1) {
			return 'ws://localhost:8888/Comic/websocket/';
		}
		//wss://www.feifeicenter.club:443/websocket/
		return 'ws://120.79.79.211:80/websocket/';
	},
	getBaseUrl: function() {
		if (this.isAborad) {
			return this.AbroadRoot
		} else if (this.isProduct != 1) {
			return this.LocalUrlRoot
		}
		return this.ChinaURLRoot
	},
	//切换服务器
	changeService: function() {
		if (this.isAborad == 1) {
			this.isAborad = 0
		} else {
			this.isAborad = 1
		}
	},
	// axios请求
	httpApi: function(method, url, params) {
		console.log('外部url' + url);
		let rooturl = this.getBaseUrl()
		return new Promise((resolve, reject) => {
			axios({
				headers: {
					'platform': 'Mobile-JS'
				},
				method: method,
				url: url,
				data: method === 'POST' || method === 'PUT' ? params : null,
				params: method === 'GET' || method === 'DELETE' ? params : null,
				baseURL: rooturl,
				withCredentials: false
			}).then(
				(response) => {
					resolve(response)
				}
			).catch(
				(error) => {
					reject(error)
				}
			)
		})
	}
}

export default HttpUtil
